package org.taptwo.android.widget.viewflow.example;

import java.util.List;

import org.taptwo.android.widget.CircleFlowIndicator;
import org.taptwo.android.widget.ViewFlow;

import android.content.res.Configuration;
import android.util.TypedValue;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.ConfigKey;
import com.epoint.mobileoa.bizlogic.GVIcon;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.db.DBHelp;
import com.epoint.mobileoa.ui.main.MainView;
import com.epoint.mobileoa.ui.main.MainViewGvAdapter;
import com.epoint.mobileoa.ui.main.MainViewIconInstance;

/**
 * Copyright © 1998 - 2012 Epoint. All Rights Reserved.
 * @author liyc
 * @time 2012-11-27 下午10:56:00
 * @annotation
 */
public class MainViewFlow extends SuperView {

	private ImageView ivbg;
	private LinearLayout lltop;
	private ViewFlow viewFlow;
	private GridView gv;
	List<GVIcon> list;
	MainViewGvAdapter adapter;
	FrameLayout flow;
	@Override
	public void initControls() {
		flow = (FrameLayout) findViewById(R.id.flow);
		gv = (GridView) findViewById(R.id.gv);
		
		ivbg = (ImageView) findViewById(R.id.ivbg);
		lltop = (LinearLayout) findViewById(R.id.lltop);
		top_left_bt.setVisibility(View.GONE);
		top_title.setText(getString(R.string.prjname));
		top_title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
		topmenu.setVisibility(View.GONE);
		list = initIcon();
		adapter = new MainViewGvAdapter(this, list);
		gv.setAdapter(adapter);
		gv.setOnItemClickListener(this);
		//UserAction.getUserPicture(this);
		//欢迎
		MainView.showWelcome(this);
		//是否开通提示
		MainView.Tip(this);
		//检测更新
		MainView.ShowUpdate(this);
		viewFlow = (ViewFlow) findViewById(R.id.viewflow);
		CircleFlowIndicator indic = (CircleFlowIndicator) findViewById(R.id.viewflowindic);
		viewFlow.setFlowIndicator(indic);
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO 自动生成的方法存根
		super.onItemClick(parent, view, position, id);
		startActivity(list.get(position).intent);
	}

	public List<GVIcon> initIcon()
	{
		return new MainViewIconInstance().iconDataSource(this);
	}

	@Override
	public void refresh(Object... param) throws Exception {
	}

	@Override
	public int getLayoutInt() {
		return R.layout.circle_layout_indicator_bottom;
	}

	@Override
	public void KEYCODE_BACK() {
		MainView.quitApp(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		
		String displayType = DBHelp.getConfigValue(this, ConfigKey.displayType);

		if(displayType.equals("0")||displayType.equals("")){
			gv.setVisibility(View.VISIBLE);
			flow.setVisibility(View.GONE);
		}
		else if(displayType.equals("1")){
			gv.setVisibility(View.GONE);
			flow.setVisibility(View.VISIBLE);
		}
		
		//设置登陆页背景主题
		if(getBackgroundStr().equals("background")){
			ivbg.setImageResource(R.drawable.background);
			lltop.setBackgroundResource(R.drawable.toolbar_bg);
		}
		else{
			ivbg.setImageResource(R.drawable.mainbg);
			lltop.setBackgroundResource(R.drawable.top_back_image);
		}
	
		adapter.notifyDataSetChanged();
		viewFlow.setAdapter(new MainViewCircleAdapter(this,initIcon()),0);
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		viewFlow.onConfigurationChanged(newConfig);
	}

	@Override
	public void refreshForNewTask(int TaskId, Object... param){
		
	}
}
